<?php
namespace app\index\controller;

use \think\Db;
use think\facade\Lang;
use app\models\ProfitLog;
use app\models\User;
use think\facade\Cache;


class Index extends Base
{
    public function trend()
    {   
        $data = $this->request->post();
        $days = isset($data['days']) ? $data['days'] : 7;
        $region = isset($data['region_id']) ? $data['region_id'] : 1;
        $list = Db::name('total_profit')->order('id','desc')->where('type',$region)->limit($days)->select();
        return $this->sendSuccess('区域走势',$list);
    }

    public function banner()
    {
        return $this->sendSuccess('banner',Db::name('banner')->select());
    }

    public function notice()
    {
        $page = $this->request->post('page') ? : 0;
        $pageSize = $this->request->post('pageSize') ? :1;
        $is_top = $this->request->post('is_top');
        $obj = Db::name('notice');
        if( $is_top ){
            $obj->where('is_top',$is_top);
        }
        $list = $obj->order('id','desc')->paginate($pageSize,false,['page'=>$page]);
        return $this->sendSuccess('公告',$list);
    }

    public function noticeRandom()
    {
        $pageSize =2;
        $max = Db::name('notice')->where('is_top',1)->count();
        $startMax = $max-$pageSize;
        if ($startMax<3){
            $list =Db::name('notice')->select();
            return $this->sendSuccess('公告',$list);
        }
        $start= rand(1, $startMax);
        $list = Db::name('notice')->order('id','desc')->limit($start,$pageSize)->select();
        return $this->sendSuccess('公告',$list);
    }

    public function version()
    {
        return $this->sendSuccess('version',[
            'version' => get_config('version'),
            'android' => get_config('android'),
            'ios' => get_config('ios'),
        ]);
    }

    public function invite()
    {
        if( $this->request->isPost() ){
            $url = 'http://'.$this->request->host().url('/invite',[
                'code'=>$this->request->user['invite_code']
            ]);
            return $this->sendSuccess('invite',['url'=>$url]);
        }
        return view();
    }

    public function fee()
    {
        return $this->sendSuccess('fee',[
            'atou' => get_config('atou'),
            'utoa' => get_config('utoa'),
            'transfer' => get_config('transfer'),
            'carry' => get_config('carry'),
            'rate' => get_config('rate')*100,
        ]);
    }
    public function type()
    {
        switch ($this->request->header('language')) {
            case 'en-us':
                Lang::load('../application/lang/en-us.php');
                break;
        }
        $data = config('type.');
        foreach($data as &$v){
            $v = lang($v);
        }

        return $this->sendSuccess('type',$data);
    }

    public function quotation()
    {
        $days = $this->request->post('days');
        $list = \think\Db::name('quotation')->order('dates','desc')->where('dates','elt',date('Y-m-d'))
        ->limit($days)->select();
        return $this->sendSuccess('AYB走势',$list);
    }

    public function team()
    {
        $page = $this->request->post('page') ?: 1;
        $pageSize = $this->request->post('pageSize') ?: 10;
        $list =  Db::table('tb_user')
            ->where('parent_id',$this->request->user_id)
            ->where('major',1)
        ->field('id,username,mobile,level,created')
        ->paginate($pageSize, false, ['page' => $page]);
        return $this->sendSuccess('team',$list);
    }

    public function updateVersion()
    {
        $data = array();
        $data['apk_version']=Cache::get('config_other_apk_version');
        $data['apk_link']=Cache::get('config_other_apk_link');
        $data['apk_update_link']=Cache::get('config_other_apk_update_link');
        $data['apk_update_text']=Cache::get('config_other_apk_update_text');
        return $this->sendSuccess('版本',$data);
    }


}   